-- RewardCDKeyPanel

local ClassRef = app.mvc.createPanelClass(...)
local MAX_WORD_LEN = 20 -- 20个字符

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RewardCDKeyPanel))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end 

function ClassRef:init(data)
	self.super.init(self, data)
	if app:isTencent() then
		self:_initWithTencent()
	else
		self:_initWithNormal()	
	end
	
end

function ClassRef:onOperate(cb)
	self._onOperate = cb
end

function ClassRef:_initWithNormal()
	local textField = self._ui.TextField
	self._editText = display.replaceTextFieldWithEditBox(textField)
	self._editText:setMaxLength( MAX_WORD_LEN )

    self._ui.btn_enter:addClickEventListener(function()
		local key = self._editText:getText()
		local matchStr = string.match(key,"[A-Za-z0-9]+")

		if string.len(key) > 0 then -- 有内容
			if matchStr ~= key then
				app:popMessage( "InvalidRedeemCode" )
			else
				executeFunc(self._onOperate, "useCDKey", key)
				self._editText:setText("")
			end
		end
	end)
end

-- 非腾讯渠道 显示兑换cdkey
function ClassRef:_initWithTencent()
	local node = cc.CSLoader:createNode(Res.RewardCDKeyPanelTencent)
	self._tencentUi = display.uiDelegate(node)
	self._ui.Panel:removeAllChildren()
	self._ui.Panel:addChild(node)
	self._tencentUi.tlt:setString(L("CDKEY_string_title"))
	self._tencentUi.text:setString(L("CDKEY_string_2"))
	self._tencentUi["btn_enter/name"]:setString(L("CDKEY_string_3"))
	self._tencentUi.btn_enter:addClickEventListener(function()
		app.channelAPI:openURL("http://wjyz.qq.com/cp/a20190415cdkm/index.shtml")
	end)
end

return ClassRef